package com.hnlwh.sort;

/**
 * @program: sortingalgorithm
 * @description:    选择排序
 * @author: yuhui
 * @date: 2021-07-27 20:35
 */
public class SelectionSort {
    public static void main(String[] args) {
        Integer[] array={1,5,35,12,85,26,30,9999,45,100,65,51,22,120};
        for (int i = 0; i < array.length; i++) {

            int minIndex = i;
            for (int j = i; j < array.length; j++) { //通过循比较找到最小值索引
                if (array[minIndex]>array[j]){
                    minIndex=j;
                }
            }
            if (i!= minIndex){ // 将最小值与i交换
                int temp= array[minIndex];
                array[minIndex]= array[i];
                array[i]=temp;
            }
        }
        for (int arr: array) {
            System.out.print(arr+" ");
        }
    }
}

